package alp.starcode.nudtportal.framework.util;

import alp.starcode.common.exception.CustomException;
import alp.starcode.nudtportal.framework.constant.ProjectConst;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.security.Key;
import java.text.SimpleDateFormat;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * @program: nudt-portal
 * @description: 场馆访问加密解密
 * @author: cy
 * @create: 2024-04-01 10:21
 **/
@Slf4j
public class Des3Util {

    // 密钥
    public final static String zhengyuan_secretKey = "152A0086DAF5FF79CCC5FF79000A5546";

    // 向量(偏移量)
    public final static String zhengyuan_iv = "11524315";

    // 加解密统一使用的编码方式
    private final static String encoding = "GBK";

    /**
     * 3DES加密
     *
     * @param plainText 普通文本
     * @return
     * @throws Exception
     */
    public static String encode(String plainText, String secretKey, String iv) {
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
            byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
            return new BASE64Encoder().encode(encryptData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return plainText;
    }

    /**
     * 3DES解密
     *
     * @param encryptText 加密文本
     * @return
     * @throws Exception
     */
    public static String decode(String encryptText, String secretKey, String iv) {
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
            //byte[] decryptData = cipher.doFinal(Des3Base64.decode(encryptText));
            byte[] decryptData = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptText));
            return new String(decryptData, encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptText;
    }


    public static void main(String[] args) throws CustomException {
//        //测试数据
        //设置时间格式
        String time = new SimpleDateFormat(ProjectConst.TIME_FORMAT).format(System.currentTimeMillis());
        String data = 2003163 + "_" + time;
        String encryptData = encode(data, zhengyuan_secretKey, zhengyuan_iv);
        String decryptorData = decode("PSaDj19oSxGZ4HKlh4tb9H0iaqFzguvH", zhengyuan_secretKey, zhengyuan_iv);
        System.out.println("原始字符串：" + data);
        System.out.println("加密后的值：" + encryptData);
        System.out.println("解密后的值：" + decryptorData);
    }


}
